# FILE: WebDocumentFactory.py
# DESC: Factory implementation for WebDocument objects.

from EntityFactory import EntityFactory
from web.entity.WebDocument import WebDocument 
from web.entity.NoneEntity import NoneEntity 
from infra.logging import logger


class WebDocumentFactory(EntityFactory):

	def __init__(self, dataFilter):
		EntityFactory.__init__(self)
		self.dataFilter_ = dataFilter 
		self.id_ = -1
		self.customer_id_ = -1
		self.abs_url_ = None
		self.rel_url_ = None
		self.name_ = NoneEntity()
		self.images_ = []
		self.whenDate_ = NoneEntity()
		self.duration_ = NoneEntity()
		self.price_ = NoneEntity() 
		self.lastCrawl_ = None
		self.contentLength_ = -1


	def validate(self, argv):
		log = logger.getLogger()
		rv = True 
		if len(argv) != 11:
			rv = False
			self.errMsg_ = "Invalid number of arguments, expected 11, got " + str(len(argv)) + "."
			log.error(self.errMsg_)
		else:
			self.rel_url_ = self.dataFilter_.filter(argv[3])
			if self.rel_url_:
				self.id_ = argv[0] 
				self.customer_id_ = argv[1]
				self.abs_url_ = argv[2]
				self.name_ = argv[4]
				if not self.name_:
					self.name_ = self.rel_url_
				self.images_ = argv[5]
				self.whenDate_ = argv[6]
				self.duration_ = argv[7]
				self.price_ = argv[8]
				self.lastCrawl_ = argv[9]
				self.contentLength_ = argv[10]
			else:
				rv = False
				self.errMsg_ = "Relative url was filtered out! WebDocumentFactory can't create valid WebDocument object!"
				log.error(self.errMsg_)
		return rv


	def doCreateEntity(self, consumer, relevanceFactor):
		webDoc = WebDocument(self.id_, self.customer_id_, self.abs_url_, self.rel_url_, self.name_, self.images_, self.whenDate_, self.duration_, self.price_, self.lastCrawl_, self.contentLength_)
		if consumer:
			consumer.onWebDocument(webDoc)
		return webDoc


	def getEntityType(self):
		return "^WebDocument$"
